package com.xgggh.redpack.ui.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.ViewModelProviders;

import com.jakewharton.rxbinding3.view.RxView;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.xgggh.redpack.R;
import com.xgggh.redpack.base.ui.BaseFragment;
import com.xgggh.redpack.databinding.PersonalFragmentBinding;
import com.xgggh.redpack.domain.auth.Profile;
import com.xgggh.redpack.ui.my.MyCustomActivity;
import com.xgggh.redpack.ui.my.MyRankActivity;
import com.xgggh.redpack.ui.my.MyRedPackActivity;
import com.xgggh.redpack.ui.my.MyWalletActivity;

import javax.inject.Inject;

import dagger.android.support.AndroidSupportInjection;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Predicate;
import io.reactivex.subscribers.DisposableSubscriber;
import kotlin.Unit;
import model.Rsp;
import viewmodel.ProfileViewModel;
import viewmodel.ViewModelFactory;

public class PersonalFragment extends BaseFragment {

    PersonalFragmentBinding mBinding;
    ProfileViewModel mViewModel;
    @Inject
    ViewModelFactory factory;

    public static PersonalFragment newInstance() {
        return new PersonalFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.personal_fragment, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        //inflater.inflate(R.menu.search, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        AndroidSupportInjection.inject(this);
        super.onAttach(context);
        mViewModel = ViewModelProviders.of(this,factory).get(ProfileViewModel.class);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mBinding.twinklingRefreshLayout.setPureScrollModeOn();
        RxView.clicks(mBinding.myWallet)
                .filter(new Predicate<Object>() {
                    @Override
                    public boolean test(Object o) throws Exception {// 查看是否已经登陆了
                        Toast.makeText(getContext(), "未登陆", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                })
                //.compose(getBaseActivity().uiTransformer())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(new Consumer<Unit>() {
                    @Override
                    public void accept(Unit unit) throws Exception {
                        startActivity(new Intent(getContext(), MyWalletActivity.class));
                    }
                });

        RxView.clicks(mBinding.myRedPack)
                .compose(getBaseActivity().uiTransformer())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(unit -> startActivity(new Intent(getContext(), MyRedPackActivity.class)));
        RxView.clicks(mBinding.myRank)
                .compose(getBaseActivity().uiTransformer())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(unit -> startActivity(new Intent(getContext(), MyRankActivity.class)));
        RxView.clicks(mBinding.myCustomer)
                .compose(getBaseActivity().uiTransformer())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(unit -> startActivity(new Intent(getContext(), MyCustomActivity.class)));
        RxView.clicks(mBinding.mySetting)
                .compose(getBaseActivity().uiTransformer())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(o -> startActivity(new Intent(getContext(), MyCustomActivity.class)));
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && items.size()==0){
            getProfile();
        }
    }

    private void getProfile(){
        mViewModel.getProfile()
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
                .subscribe(new DisposableSubscriber<Rsp<Profile>>() {
            @Override
            public void onNext(Rsp<Profile> profileRsp) {
                //mBinding.
            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


/*    private  void getRedPack(final int page,int size,String sort){
        new MockRedPackService(getBaseActivity().getMockDelegate(RedPackService.class))
                //getService(RedPackService.class)
                .getSearchList(page,size,sort)
                .compose(getBaseActivity().ioTransformer())
                .subscribe(new DisposableSubscriber<PackListRsp>() {
                    @Override
                    public void onNext(PackListRsp rsp) {
                        List<Pack> redPacks= rsp.getData();
                        if (page==firstPage()){
                            if (redPacks==null || redPacks.size()==0){
                                // 没有数据，显示空界面，点击跳转发红包界面
                                return;
                            }
                            //mNavHomeViewBinder.updateData(redPacks,true);
                        }else if (page==nextPage()){
                            if (redPacks==null || redPacks.size()==0){
                                Toast.makeText(getBaseActivity(), "没有更多数据了", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            //mNavHomeViewBinder.updateData(redPacks,false);
                        }

                        if (redPacks!=null && redPacks.size()!=0){
                            setCurrentPage(page);
                        }

                    }

                    @Override
                    public void onError(Throwable t) {
                        t.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        //mNavHomeViewBinder.closeRefreshUI();
                        if (page==firstPage()){
                            //mBinding.contentHome.twinklingRefreshLayout.finishRefreshing();
                        }else{
                            //mBinding.appBarHome.contentHome.twinklingRefreshLayout.finishLoadmore();
                        }
                    }
                });
    }*/
}
